package net.chrone.quickpayapi.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;

import net.chrone.quickpayapi.model.PayforTxn;
import net.chrone.quickpayapi.model.PlatOrgInf;
import net.chrone.quickpayapi.service.PayforService;
import net.chrone.quickpayapi.service.PlatOrgInfService;
import net.chrone.quickpayapi.util.Constant;
import net.chrone.quickpayapi.util.SignatureUtil;

@Controller
@RequestMapping("/v1.0/payfor")
public class PayforController {

	private  final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private PlatOrgInfService platOrgInfService;
	@Autowired
	private PayforService payforService;
	/**
	 * 二维码支付
	 * @param merchant
	 * @param response
	 */
	@RequestMapping(value="/query",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public void payfor(@RequestBody PayforTxn order,HttpServletResponse response)throws Exception{
		logger.debug("payfortxn msg:"+order);
		Map<String,String> respMap = new HashMap<String, String>();
		if(StringUtils.isEmpty(order.getOrgId())){
			logger.error("机构号不能为空");
			respMap.put("respCode","401");
			response(respMap,response);
			return;
		}
		PlatOrgInf platOrgInf = platOrgInfService.findById(order.getOrgId());
		if(platOrgInf==null){
			logger.error("找不到机构信息,orgId:"+order.getOrgId());
			respMap.put("respCode","402");
			response(respMap,response);
			return;
		}
		boolean flag = SignatureUtil.verify(order.getSignature(), order,platOrgInf.getPublicKey());
		if(!flag){
			logger.error("验签失败");
			respMap.put("respCode","403");
			response(respMap,response);
			return;
		}
		PayforTxn txn = payforService.findByOrgOrderNo(order.getOrgId(), order.getOrderNo());
		if(txn!=null){
			respMap.put("account",txn.getAccount());
			respMap.put("amount",txn.getAmount()+"");
			respMap.put("orderNo",txn.getOrderNo());
			respMap.put("orgId",txn.getOrgId());
			respMap.put("paySt",txn.getPaySt()+"");
			respMap.put("respCode","200");
			response(respMap,response);
		}else{
			logger.error("交易不存在");
			respMap.put("respCode","418");
			response(respMap,response);
		}
		return;
	}
	
	private void response(Map<String, String> respMap,HttpServletResponse response) {
		respMap.put("respMsg", Constant.getErrorCodeMap().get(respMap.get("respCode")));
		respMap.put("signature", SignatureUtil.signResp(respMap));
		String json = JSON.toJSONString(respMap);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
